package com.bigfish.gbs.service.impl;

import com.bigfish.gbs.dao.ResourceMapper;
import com.bigfish.gbs.entity.Resource;
import com.bigfish.gbs.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Joycessh
 * @Description Api资源相关
 * @Date 12/13/18 11:14 AM
 */
@Service("ResourceService")
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 查询所有Api
     * @author Joycessh
     * @param
     * @return java.util.List<com.bigfish.gbs.entity.Resource>
     * @date 12/13/18 11:18 AM
     */
    @Override
    public List<Resource> getApiList() throws DataAccessException {
        return resourceMapper.selectApiList();
    }

    /**
     * 添加Api
     * @author Joycessh
     * @param resource
     * @return java.lang.Boolean
     * @date 12/13/18 11:21 AM
     */
    @Override
    public Boolean addApi(Resource resource) throws DataAccessException {
        int num = resourceMapper.insertSelective(resource);
        return num == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    /**
     * 更新Api
     * @author Joycessh
     * @param resource
     * @return java.lang.Boolean
     * @date 12/13/18 11:24 AM
     */
    @Override
    public Boolean updateApi(Resource resource) throws DataAccessException {
        int num = resourceMapper.updateByPrimaryKeySelective(resource);
        return num == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    /**
     * 删除Api
     * @author Joycessh
     * @param id
     * @return java.lang.Boolean
     * @date 12/13/18 11:24 AM
     */
    @Override
    public Boolean deleteApi(Integer id) throws DataAccessException {
        int num = resourceMapper.deleteByPrimaryKey(id);
        return num == 1 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public List<Resource> getAuthorityApisByRoleId(Integer roleId) throws DataAccessException {
        return resourceMapper.selectApisByRoleId(roleId);
    }

    @Override
    public List<Resource> getNotAuthorityApisByRoleId(Integer roleId) throws DataAccessException {
        return resourceMapper.selectNotAuthorityApisByRoleId(roleId);
    }
}
